﻿
$(function () {
    //初始化列表信息
    init();
});

//初始化列表信息
function init(queryData) {

    $('#department_tree2').tree({
        checkbox: false,
        url: '/Department/GetTreeList2?parentId=00000000-0000-0000-0000-000000000000',
        onBeforeExpand: function (node, param) {
            $('#department_tree2').tree('options').url = "/Department/GetTreeList2?parentId=" + node.id;// change the url                       
        },
        onClick: function (node) {
            alert(node.id);
        }
    });

    GetList();
}

function DepartmentTreeOnclick(queryData) {
    
    GetList(queryData);
}


//获取列表
function GetList(queryData) {
    $('#list').datagrid({
        idField: 'DepartmentID',
        fitColumns: 'true',
        singleSelect: true,
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'Department/GetList',
        queryParams: queryData,    //参数为了多条件查询预留
        columns: [[
            { field: 'ck', checkbox: true },
            { field: 'DepartmentID', title: 'DepartmentID', hidden: true },
            { field: 'DepartmentName', title: '部门名称', width: 180 },
            { field: 'DepartmentParentName', title: '上级部门', width: 180 },
            { field: 'ManageUserID', title: '部门呢主管', width: 180 },
            { field: 'Telephone', title: '电话', width: 180 },
            { field: 'Address', title: '地址', width: 180 }

        ]]
    });
}

//弹出增加窗口
function add() {
    $('#dlg').dialog('open').dialog('setTitle', '增加');
    $('#fm').form('clear');
    var node = $('#department_tree').tree('getSelected');
    if (node) {
        $('#DepartmentParentID').val(node.id);//用于点击添加的时候给隐藏域以及父级部门赋值
        $('#comb_DepartmentParentID').combotree('setValue', node.id);
    }
    url = '/Department/Create';
}

//弹出修改窗口
function edit() {
    $('#fm').form('clear');
    var rows = $("#list").datagrid("getSelections");
    if (rows.length == 1) {
        var updateID = rows[0].DepartmentID;
        var myDate = new Date().getTime();//防止数据缓存
        $.getJSON("/Department/GetEntity?date=" + myDate, { ID: updateID }, function (data) {
            //绑定数据显示到用户控件上面
            $('#fm').form('load', data);
            $('#comb_DepartmentParentID').combotree('setValue', data.DepartmentParentID);
            //获取主管姓名
            $.getJSON("/User/GetEntity", { ID: data.ManageUserID }, function (userdata) {
                $('#ManageUserName').val(userdata.EmployeeName);
            });
        })
        $('#dlg').dialog('open').dialog('setTitle', '修改');
        url = '/Department/Edit';
    }
    else {
        $.messager.alert("友情提示", "每次只能修改一行数据，你已经选择了<font color='red' size='6'>" + UpdateDictionaryInfoID.length + "</font>行");
    }
}

//保存数据
function save() {
    $('#fm').form('submit', {
        url: url,
        onSubmit: function () {
            return $(this).form('validate');
        },
        success: function (result) {
            if (result == 'OK') {
                $('#dlg').dialog('close');      // close the dialog  
                $('#list').datagrid('reload');    // reload the user data  
            } else {
                $.messager.show({
                    title: 'Error',
                    msg: result
                });
            }
        }
    });
}
//删除
function del() {
    var rows = $('#list').datagrid('getSelected');
    if (rows) {
        $.messager.confirm(delDlgTitle, delDlgMsg, function (r) {
            if (r) {
                $.post('/Department/Delete', { id: rows.DepartmentID },
                    function (result) {
                        if (result == 'OK') {
                            $("#list").datagrid('clearSelections');
                            $('#list').datagrid('reload');    // reload the user data  
                        } else {
                            $.messager.show({   // show error message  
                                title: 'Error',
                                msg: result
                            });
                        }
                    });
            }
        });
    }
}

//获取用户列表
function GetUserList(queryData) {
    $('#userdlg').dialog('open').dialog('setTitle', '选择部门主管');
    $('#userlist').datagrid({
        title: '用户列表',
        idField: 'UserID',
        fitColumns: 'true',
        singleSelect: true,
        autoRowHeight: true,
        nowrap: true,
        striped: true,
        pagination: true,
        url: 'User/GetList',
        queryParams: queryData,    //参数为了多条件查询预留
        columns: [[
			{ field: 'ck', checkbox: true },
			{ field: 'UserID', title: 'ID', hidden: true },
			{ field: 'UserName', title: '账号', width: 180 },
			{ field: 'EmployeeName', title: '姓名', width: 180 },
			{ field: 'DepartmentID', title: '部门', width: 180 },
			{ field: 'RoleID', title: '角色（职务）', width: 180 },
			{ field: 'Telephone', title: '固定电话', width: 180 }
        ]],
    });
}

//选择部门主管
function SelectUser() {
    var rows = $("#userlist").datagrid("getSelections");
    if (rows.length == 1) {
        var userID = rows[0].UserID;
        var employeeName = rows[0].EmployeeName;
        $("#ManageUserID").val(userID);
        $("#ManageUserName").val(employeeName);
        $('#userdlg').dialog('close');
    }
    else {
        $.messager.alert("友情提示", "请选择部门主管");
    }
}

//根据部门ID，用户名查询用户
function Search() {
    var depatmentID = $('#comb_Search_DepartmentParentID').combotree('getValue');
    var username = $('#userName').val();
    
    if (depatmentID == '请选择部门') {
        depatmentID = '';
    }
    if (username == '用户名称') {
        username = '';
    }
    var querData = {
        j_depatmentID: depatmentID,
        j_username: username
    };

    GetUserList(querData);
}